//
//  HomeUpdateView.m
//  YXFinance
//
//  Created by uplooking on 2018/10/26.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "HomeUpdateView.h"

@interface HomeUpdateView()
{
    ConfigureModel * configureModel;
}
@end

@implementation HomeUpdateView


- (void)initView{
    
    [self removeAllSubviews];

    [self setBackgroundColor:HexRGBA(0x000000, 0.5)];
    
    UIImageView * imageView = [[UIImageView alloc]init];
    [imageView setUserInteractionEnabled:YES];
    [imageView.layer setMasksToBounds:YES];
    [imageView.layer setCornerRadius:8.0 * kAutoScanleX];
    [self addSubview:imageView];
    [imageView setImage:[UIImage imageNamed:@"bg_banben_genxin"]];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(289 * kAutoScanleX, 369 * kAutoScanleX));
    }];

    
    
    UILabel * labelTitle = [[UILabel alloc]init];
    [labelTitle setText:configureModel.updataTitle];
    [labelTitle setTextColor:HexRGBA(0x000000, 1)];
    [labelTitle setFont:[UIFont systemFontOfSize:16 * kAutoScanleX]];
    [labelTitle setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:labelTitle];
    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imageView);
        make.top.equalTo(imageView).offset(180 * kAutoScanleX);
    }];
    
    
    UIView * lastView = nil;
    
    for (NSInteger i = 0; i < 2; i ++) {
        
        UIView * item = [[UIView alloc]init];
        [imageView addSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastView == nil ? labelTitle.mas_bottom : lastView.mas_bottom).offset(lastView == nil ? 15 * kAutoScanleX : 5 * kAutoScanleX);
            make.left.right.equalTo(imageView);
        }];
        
      
        UILabel * labelTitle = [[UILabel alloc]init];
        [labelTitle setNumberOfLines:0];
        [labelTitle setTextColor:HexRGBA(0x585858, 1)];
        [labelTitle setFont:[UIFont systemFontOfSize:12 * kAutoScanleX]];
        [labelTitle setTextAlignment:NSTextAlignmentLeft];
        [self addSubview:labelTitle];
        [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(item);
            make.left.equalTo(item).offset(45 * kAutoScanleX);
        }];
        
        if (i == 0) {
            
            [labelTitle setText:configureModel.updatabody1];
            
        }else if (i == 1){
            
            [labelTitle setText:configureModel.updatabody2];
        }
        
        UIView * fView = [[UIView alloc]init];
        [item addSubview:fView];
        [fView.layer setMasksToBounds:YES];
        [fView.layer setCornerRadius:2.5 * kAutoScanleX];
        [fView setBackgroundColor:HexRGBA(0xbc9a6d, 1)];
        [fView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(labelTitle);
            make.right.equalTo(labelTitle.mas_left).offset(-5 * kAutoScanleX);
            make.size.mas_equalTo(CGSizeMake(5 * kAutoScanleX, 5 * kAutoScanleX));
        }];
        
        
        [item mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(labelTitle);
        }];
        
        lastView = item;
    }
    
    UIButton * fBtn=[[UIButton alloc]init];
    [fBtn setBackgroundColor:HexRGBA(0xb99c6d, 1)];
    [fBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [fBtn addTarget:self action:@selector(fBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [fBtn setTitle:@"马上更新" forState:UIControlStateNormal];
    [fBtn.titleLabel setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
    [imageView addSubview:fBtn];
    [fBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(imageView);
        make.left.equalTo(imageView).offset(3.5 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(289/2.0 * kAutoScanleX, 49 * kAutoScanleX));
    }];
    
    UIButton * rBtn=[[UIButton alloc]init];
    [rBtn setBackgroundColor:HexRGBA(0xe5e5e5, 1)];
    [rBtn setTitleColor:HexRGBA(0x424448, 1) forState:UIControlStateNormal];
    [rBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [rBtn setTitle:@"放弃更新" forState:UIControlStateNormal];
    [rBtn.titleLabel setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
    [imageView addSubview:rBtn];
    [rBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(imageView);
        make.right.equalTo(imageView).offset(-3.5 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(283/2.0 * kAutoScanleX, 49 * kAutoScanleX));
    }];

    
}

- (void)setModel:(ConfigureModel *)model{
    
    configureModel = model;
    
    if (model) {
        
        [self initView];
        
    }else{
        
        [self removeFromSuperview];
    }
    
}


- (void)fBtnAction:(UIButton *)sender{
    
    NSString * str = [UserCenterHelper shareInstance].configureModel.appstore_url;

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{@"":@""} completionHandler:nil];
    
    [self removeFromSuperview];
    
}

- (void)backAction:(UIButton *)sender{
    
    [self removeFromSuperview];
    
}


@end
